package com.csu54sher.basereservation.service.impl;

import com.csu54sher.basereservation.dto.CommentSubmitDTO;
import com.csu54sher.basereservation.entity.Comment;
import com.csu54sher.basereservation.mapper.CommentMapper;
import com.csu54sher.basereservation.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu54sher.basereservation.service.IReservationService;
import com.csu54sher.basereservation.service.IUserService;
import com.csu54sher.basereservation.util.ThreadLocalUtil;
import com.csu54sher.basereservation.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 评价表 服务实现类
 * </p>
 *
 * @author csu54sher
 * @since 2025-10-19
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    private IReservationService reservationService;

    @Autowired
    private IUserService userService;

    @Override
    public void comment(Integer reservationId, CommentSubmitDTO commentSubmitDTO) {
        String currentId = ThreadLocalUtil.getCurrentId();//获取当前用户学号或者工号
        Long userId = userService.query().eq("id_number", currentId).one().getId();//获取当前用户ID
        Integer baseId = reservationService.query().eq("id", reservationId).one().getBaseId();
        Comment comment = Comment.builder()
                .id(userId)
                .baseId(baseId)
                .comment(commentSubmitDTO.getComment())
                .environmentScore(commentSubmitDTO.getEnvironmentScore())
                .explanationScore(commentSubmitDTO.getExplanationScore())
                .processScore(commentSubmitDTO.getProcessScore())
                .totalScore(commentSubmitDTO.getTotalScore())
                .build();
        save( comment);
    }

    @Override
    public List<CommentVO> getCommentByBaseId(Integer baseId) {
        if (baseId == null) {
            return Collections.emptyList();
        }
        List<Comment> comments = query().eq("base_id", baseId).list();
        if (comments == null) {
            return Collections.emptyList();
        }

        List<CommentVO> commentVOS = new ArrayList<>(comments.size());
        for (Comment comment : comments) {
            String currentId = userService.query().eq("id", comment.getUserId()).one().getIdNumber();
            String name = userService.query().eq("id_number", currentId).one().getName();
            commentVOS.add(CommentVO.builder()
                    .id(Math.toIntExact(comment.getId()))
                    .idNumber(currentId)
                    .name(name)
                    .comment(comment.getComment())
                    .environmentScore(comment.getEnvironmentScore())
                    .explanationScore(comment.getExplanationScore())
                    .processScore(comment.getProcessScore())
                    .totalScore(comment.getTotalScore())
                    .createAt(comment.getCreateAt())
                    .build());
        }
        return commentVOS;
    }

    @Override
    public List<CommentVO> getCommentByReservationId(Integer reservationId) {
        if (reservationId == null){
            return Collections.emptyList();
        }
        List<Comment> comments = query().eq("reservation_id", reservationId).list();
        if (comments == null) {
            return Collections.emptyList();
        }
        List<CommentVO> commentVOS = new ArrayList<>(comments.size());
        for (Comment comment : comments) {
            String currentId = userService.query().eq("id", comment.getUserId()).one().getIdNumber();
            String name = userService.query().eq("id_number", currentId).one().getName();
            commentVOS.add(CommentVO.builder()
                    .id(Math.toIntExact(comment.getId()))
                    .idNumber(currentId)
                    .name(name)
                    .comment(comment.getComment())
                    .environmentScore(comment.getEnvironmentScore())
                    .explanationScore(comment.getExplanationScore())
                    .processScore(comment.getProcessScore())
                    .totalScore(comment.getTotalScore())
                    .createAt(comment.getCreateAt())
                    .build());
        }
        return commentVOS;
    }

    @Override
    public List<CommentVO> getComments() {
        List<Comment> comments = query().list();
        if (comments == null) {
            return Collections.emptyList();
        }
        List<CommentVO> commentVOS = new ArrayList<>(comments.size());
        for (Comment comment : comments) {
            String currentId = userService.query().eq("id", comment.getUserId()).one().getIdNumber();
            String name = userService.query().eq("id_number", currentId).one().getName();
            commentVOS.add(CommentVO.builder()
                    .id(Math.toIntExact(comment.getId()))
                    .idNumber(currentId)
                    .name(name)
                    .comment(comment.getComment())
                    .environmentScore(comment.getEnvironmentScore())
                    .explanationScore(comment.getExplanationScore())
                    .processScore(comment.getProcessScore())
                    .totalScore(comment.getTotalScore())
                    .createAt(comment.getCreateAt())
                    .build());
        }
        return commentVOS;


    }
}
